define(function (require, exports, module) {
    var base = require('base');
    // 通过 require 引入依赖
    var F = module.exports = {
        basepath: '',
        selectors:{
            dg:$('#main-user-manage-tb'),
            addDialog:$('#main-user-manage-add-dialog'),
            addDialogForm:$('#main-user-manage-add-dialog-form'),
            addDialogDepsComboTree:$('#main-user-manage-add-dialog-form input[name=dep]')
        },
        init: function (_basepath) {
            F.basepath = _basepath;

            window.deleteuser= F.delete;

            var config={
                url: F.basepath+"/user/get-manage-user.html",
                fitColumns:true,
                idField:'id',
                striped:true,
                pagination:true,
                rownumbers:true,
                singleSelect:true,
                loadFilter:function(data){
                    if(data.ok){
                        return data.data;
                    }
                },
                onLoadError: function (arg) {
                    base.loadError(arg);
                },
                onLoadSuccess: function (data) {
                    $('.easyui-linkbutton').linkbutton();
                    if (base.perList.user.edit||base.perList.user.del){
                        F.selectors.dg.datagrid("showColumn", "action");
                    }
                },
                columns:[[
                    {field:'id',title:'账号id',width:'10%',hidden:true},
                    {field:'account',title:'账号',width:'15%'},
                    {field:'dep_name',title:'部门名字',width:'20%'},
                    {field:'roleNames',title:'角色名字',width:'20%'},
                    {field:'action',title:'操作',width:'20%',hidden:true, formatter: function (value, row, index) {
                        var action="";
                        if(base.perList.user.edit) {
                            action += "<a href='javascript:void(0)' plain='true' class='easyui-linkbutton' iconCls='icon-edit'>编辑用户</a>";
                        }
                        if(base.perList.user.del) {
                            action += "<a href='javascript:void(0)' plain='true' onclick='deleteuser("+row.id+")' class='easyui-linkbutton' iconCls='icon-remove'>删除用户</a>";
                        }
                        return action;
                    }}
                ]]
            };

            if (base.perList.user.create) {
                config.toolbar = [
                    {
                        iconCls: 'icon-add',
                        text: '添加用户',
                        handler: function () {
                            F.selectors.addDialog.show().dialog({
                                cache: false,
                                onClose: function () {
                                    F.selectors.addDialogForm.form('clear');
                                },
                                onOpen: function () {
                                    F.selectors.addDialogDepsComboTree.combotree({
                                        url: F.basepath+"/main/department/get-show-departments.html",
                                        loadFilter: function (data) {
                                            if (data.ok) {
                                                return data.data;
                                            }
                                            base.ajaxSuccessData(data);
                                        }
                                    })
                                    //表单初始化
                                    F.selectors.addDialogForm.form({
                                            url: F.basepath + '/user/create.html',
                                            ajax: true,
                                            onSubmit: function (param) {
                                                param.ajax = 'true';
                                                return F.selectors.addDialogForm.form('validate');
                                            },
                                            success: function (data) {
                                                data = $.parseJSON(data);
                                                base.ajaxSuccessData(data);
                                                if (data.ok) {
                                                    F.selectors.addDialog.dialog('close');
                                                    F.selectors.dg.datagrid('load');
                                                }
                                            },
                                            onLoadError: function () {
                                                base.loadError('');
                                            }
                                        }
                                    );

                                },
                                buttons: [{
                                    text: '添加',
                                    iconCls: 'icon-ok',
                                    handler: function () {
                                        F.selectors.addDialogForm.form('submit');
                                    }
                                }]
                            });
                        }
                    }
                ];
            }

            if(base.perList.user.check) {
                F.selectors.dg.datagrid(config);
            }
        },
        delete:function(id){
            F.selectors.dg.datagrid('selectRecord',id);
            var selectRow = F.selectors.dg.datagrid('getSelected');
            $.messager.confirm('提示', '你确定要删除用户『' + selectRow.account + '』吗？', function (r) {
                if (r) {
                    base.ajaxRequest(F.basepath + "/user/delete.html", {"userId": id}, function (data) {
                        base.ajaxSuccessData(data);
                        F.selectors.dg.datagrid('load');
                    }, false, "post");
                }
            });
        }
    }
}
);